﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Xsl;
using System.Xml;

namespace VISS.MailImpl {
    public class TemplateBuilder {
        public static string GetResourceContent(string sAssemblyResourceName) {
            Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(sAssemblyResourceName);
            string sContent = "";
            if(stream != null) {
                StreamReader s = new StreamReader(stream);
                sContent = s.ReadToEnd();
                return sContent;
            }
            return "";
        }
        public static Stream GetResourceStream(string sAssemblyResourceName) {
            Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(sAssemblyResourceName);
            return stream;
        }

        public static string GetForgotPasswordSuccessTemplate(string userKey, string fullName, string email, string hyperlink) {
            XsltArgumentList xslarg = new XsltArgumentList();
            XmlDocument xdoc = new XmlDocument();

            XslCompiledTransform xsl = new XslCompiledTransform();

            xslarg.AddParam("UserKey", "", userKey);
            xslarg.AddParam("FullName", "", fullName);
            xslarg.AddParam("Email", "", email);
            xslarg.AddParam("HyperLink", "", hyperlink);
            xslarg.AddParam("AntiSpam", "", "<a href=\"http://viss.vn?email=" + email + "\" style=\"font-weight: bold; text-decoration: none; color: #ffffff;\">VISS.VN</a>");

            System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(GetResourceStream("VISS.MailImpl.Templates.ForgotPassword.xslt"));
            xsl.Load(xmlReader);

            StringWriter sw = new StringWriter();
            xsl.Transform(xdoc, xslarg, sw);

            string tmp = sw.ToString().Replace("xmlns:asp=\"remove\"", "").Replace("<", "<").Replace(">", ">");
            tmp = tmp.Replace("|0", "<").Replace("0|", ">");
            tmp = tmp.Replace("&lt;", "<");
            tmp = tmp.Replace("&gt;", ">");

            sw.Close();

            return tmp;
        }
        
        public static string GetRegisterSuccessTemplate(string userKey, string fullName, string email, string password)
        {
            XsltArgumentList xslarg = new XsltArgumentList();
            XmlDocument xdoc = new XmlDocument();

            XslCompiledTransform xsl = new XslCompiledTransform();

            xslarg.AddParam("UserKey", "", userKey);
            xslarg.AddParam("FullName", "", fullName);
            xslarg.AddParam("Email", "", email);
            xslarg.AddParam("Password", "", password);
            xslarg.AddParam("AntiSpam", "", "<a href=\"http://viss.vn?email=" + email + "\" style=\"font-weight: bold; text-decoration: none; color: #ffffff;\">VISS.VN</a>");

            System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(GetResourceStream("VISS.MailImpl.Templates.RegisterSuccess.xslt"));
            xsl.Load(xmlReader);

            StringWriter sw = new StringWriter();
            xsl.Transform(xdoc, xslarg, sw);

            string tmp = sw.ToString().Replace("xmlns:asp=\"remove\"", "").Replace("<", "<").Replace(">", ">");
            tmp = tmp.Replace("|0", "<").Replace("0|", ">");
            tmp = tmp.Replace("&lt;", "<");
            tmp = tmp.Replace("&gt;", ">");

            sw.Close();

            return tmp;
        }

        public static string GetVerifiedNotificationTemplate(string userKey, string fullName, string email) {
            XsltArgumentList xslarg = new XsltArgumentList();
            XmlDocument xdoc = new XmlDocument();

            XslCompiledTransform xsl = new XslCompiledTransform();

            xslarg.AddParam("UserKey", "", userKey);
            xslarg.AddParam("FullName", "", fullName);
            xslarg.AddParam("AntiSpam", "", "<a href=\"http://viss.vn?email=" + email + "\" style=\"font-weight: bold; text-decoration: none; color: #ffffff;\">VISS.VN</a>");

            System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(GetResourceStream("VISS.MailImpl.Templates.VerifiedNotification.xslt"));
            xsl.Load(xmlReader);

            StringWriter sw = new StringWriter();
            xsl.Transform(xdoc, xslarg, sw);

            string tmp = sw.ToString().Replace("xmlns:asp=\"remove\"", "").Replace("<", "<").Replace(">", ">");
            tmp = tmp.Replace("|0", "<").Replace("0|", ">");
            tmp = tmp.Replace("&lt;", "<");
            tmp = tmp.Replace("&gt;", ">");

            sw.Close();

            return tmp;
        }

        public static string GetUnVerifyNotificationTemplate(string userKey, string fullName, string email) {
            XsltArgumentList xslarg = new XsltArgumentList();
            XmlDocument xdoc = new XmlDocument();

            XslCompiledTransform xsl = new XslCompiledTransform();

            xslarg.AddParam("UserKey", "", userKey);
            xslarg.AddParam("FullName", "", fullName);
            xslarg.AddParam("AntiSpam", "", "<a href=\"http://viss.vn?email=" + email + "\" style=\"font-weight: bold; text-decoration: none; color: #ffffff;\">VISS.VN</a>");

            System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(GetResourceStream("VISS.MailImpl.Templates.UnVerifyNotification.xslt"));
            xsl.Load(xmlReader);

            StringWriter sw = new StringWriter();
            xsl.Transform(xdoc, xslarg, sw);

            string tmp = sw.ToString().Replace("xmlns:asp=\"remove\"", "").Replace("<", "<").Replace(">", ">");
            tmp = tmp.Replace("|0", "<").Replace("0|", ">");
            tmp = tmp.Replace("&lt;", "<");
            tmp = tmp.Replace("&gt;", ">");

            sw.Close();

            return tmp;
        }

        public static string GetMessagesNotificationTemplate(string userKey, string fullName, string email, int messagesInDay) {
            XsltArgumentList xslarg = new XsltArgumentList();
            XmlDocument xdoc = new XmlDocument();

            XslCompiledTransform xsl = new XslCompiledTransform();

            xslarg.AddParam("UserKey", "", userKey);
            xslarg.AddParam("FullName", "", fullName);
            xslarg.AddParam("MessagesInDay", "", messagesInDay);
            xslarg.AddParam("AntiSpam", "", "<a href=\"http://viss.vn?email=" + email + "\" style=\"font-weight: bold; text-decoration: none; color: #ff0051;\">đây</a>");

            System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(GetResourceStream("VISS.MailImpl.Templates.MessagesNotification.xslt"));
            xsl.Load(xmlReader);

            StringWriter sw = new StringWriter();
            xsl.Transform(xdoc, xslarg, sw);

            string tmp = sw.ToString().Replace("xmlns:asp=\"remove\"", "").Replace("<", "<").Replace(">", ">");
            tmp = tmp.Replace("|0", "<").Replace("0|", ">");
            tmp = tmp.Replace("&lt;", "<");
            tmp = tmp.Replace("&gt;", ">");

            sw.Close();

            return tmp;
        }

        public static string GetNotificationTemplate(string userKey, string fullName, string email, string title, string content) {
            XsltArgumentList xslarg = new XsltArgumentList();
            XmlDocument xdoc = new XmlDocument();

            XslCompiledTransform xsl = new XslCompiledTransform();

            xslarg.AddParam("UserKey", "", userKey);
            xslarg.AddParam("FullName", "", fullName);
            xslarg.AddParam("Title", "", title);
            xslarg.AddParam("Content", "", content);
            xslarg.AddParam("AntiSpam", "", "<a href=\"http://viss.vn?email=" + email + "\" style=\"font-weight: bold; text-decoration: none; color: #ff0051;\">đây</a>");

            System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(GetResourceStream("VISS.MailImpl.Templates.Notification.xslt"));
            xsl.Load(xmlReader);

            StringWriter sw = new StringWriter();
            xsl.Transform(xdoc, xslarg, sw);

            string tmp = sw.ToString().Replace("xmlns:asp=\"remove\"", "").Replace("<", "<").Replace(">", ">");
            tmp = tmp.Replace("|0", "<").Replace("0|", ">");
            tmp = tmp.Replace("&lt;", "<");
            tmp = tmp.Replace("&gt;", ">");
            sw.Close();

            return tmp;
        }

      
    }
}
